Next we need to start eclipse and create a workspace.

Create Initial Project

  1. Start eclipse.
  2. When prompted, enter a directory for your workspace. I used C:\workspaces\JpaAndEjb3. To understand why I recommend not using a space in the name, read this sidebar.
  3. Close the Welcome window

User Library

We are going to define a user library, which is just a collection of jar files with a name. Once we create this, we can add it to our classpath with one command. This also makes setting up new projects in the same workspace a snap. We can also export workspace settings and import them into a new workspace.
  1. Pull down Window:Preferences
  2. Navigate to Java:Build Path:User Libraries
  3. Click on New
  4. Enter JPA_JSE for the name and click on OK

Now we need to add several jars to this list. For each of the following jars, do the following:
  1. Select JPA_JSE (after you add the first one, you'll have to go back and click the library, which seems to be a poor UI design)
  2. Click on Add JARs...
  3. Navigate to the jar file
  4. Select the jar file
  5. Click on Open
  6. Repeat at step one.

Here is a list of all the jar files you'll need to add (note the path's listed assume you extracted your jar files to C:/libs):
  • C:/libs/hibernate-distribution-3.3.1.GA/lib/required/antlr-2.7.6.jar
  • C:/libs/hibernate-distribution-3.3.1.GA/lib/required/commons-collections-3.1.jar
  • C:/libs/hibernate-distribution-3.3.1.GA/lib/required/dom4j-1.6.1.jar
  • C:/libs/hibernate-distribution-3.3.1.GA/lib/required/javassist-3.4.GA.jar
  • C:/libs/hibernate-distribution-3.3.1.GA/hibernate3.jar
  • C:/openejb-3.1.1/lib/javaee-api-5.0-2.jar
  • C:/openejb-3.1.1/lib/log4j-1.2.12.jar
  • C:/hibernate-entitymanager-3.4.0.GA/hibernate-entitymanager.jar
  • C:/hsqldb-1.9.0-beta3/hsqldb/lib/hsqldb.jar
  • C:/hibernate-annotations-3.4.0.GA/hibernate-annotations.jar
  • C:/hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
  • C:/slf4j-1.5.8/slf4j-simple-1.5.8.jar
  • C:/slf4j-1.5.8/slf4j-api-1.5.8.jar

Create Java Project

Next we need to create a Java project. We'll keep the source separate from the bin directory:
  1. Pull down the File menu and select New:Project
  2. Select Java Project and click Next
  3. Enter a project name: JpaTutorial1, again read this sidebar to know why I did not use a space in the project name.
  4. Make sure "Create new project in workspace" is selected.
  5. Make sure the JRE selected is 1.5.x. If a 1.5 JRE does not show in the list, you can add it through Window->Preferences->JAVA->Installed JRE's.
  6. Select "Create separate source and output folders"
  7. Click "finish"

Create folders and packages

  1. Expand your project JpaTutorial1
  2. Select the src folder
  3. Right-click, select new:Folder
  4. Enter the name META-INF
  5. Click Finish
  6. Select the src folder again
  7. Right-click, select new:Package
  8. Enter the name entity
  9. Click on Finish
  10. Select the Tutoria1 project again
  11. Right-click, select new:Source Folder
  12. Enter the name test
  13. Click Finish
  14. Select the test folder
  15. Right-click, select new:Package
  16. Enter the name entity

Add Required Libraries

We now need to add two libraries. One will be the user-defined library we created above. The second will be JUnit 4.x.

  1. Edit the project properties. Select your project (e.g. JpaTutorial1) and either press alt-enter or right-click and select properties.
  2. Select Java Build Path
  3. Click on the Libraries tab
  4. Click on Add Library
  5. Select User Libraries and click Next
  6. Select JPA_JSE by clicking on the checkbox
  7. Click OK
  8. Click on Add Library again
  9. Click on JUnit
  10. Click Next
  11. In the pull-down list, select JUnit 4
  12. Click Finish
  13. Click OK


Sidebar

The JPA specification says that in a managed environment (read as running in the container), you do not need to list your entity classes in the persistence.xml (this is coming up). When you're using JPA in a JSE environment, this is not guaranteed. In all of these examples, we're using the hibernate implementation of the JEE Entity Manager. It provides the functionality of automatically registering all of your entities without your having to explicitly list them. However, if you happen to have a space in your class path, it appears to fail.

If you followed the instructions above, you'll have the following directory: C:/Workspaces/JpaAndEjb3/JpaTutorial1. Under that directory will be a bin directory where our compiled class files reside. Hibernate will look in this directory and find all classes that are entities (denoted with @Entity) and add those to our persistent unit. In the first version of this tutorial, I recommended the following name: C:/Workspaces/Jpa And Ejb3/Tutorial 1. When I ran the driver program, hibernate was unable to automatically find the bin directory, I assume this was because of the spaces in the name. I changed the name by removing all of the spaces and the problem went away.